package de.preamp.clients.java.utils;

import java.awt.AWTException;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import net.roarsoftware.lastfm.Caller;
import de.preamp.clients.java.utils.ui.MainUI;
import de.preamp.clients.java.utils.ui.PreampTray;
import de.preamp.clients.java.utils.ui.PreferencesUI;

/**
 * Main class for starting the program.
 * 
 * @author André Stannek
 * @version 2010-08-22
 */
public final class Main
{

    /**
     * Main method for startup.
     * 
     * @param args
     *            arguments
     */
    public static void main(final String[] args)
    {
	Caller.getInstance().setUserAgent("pre");
	if (AbstractPreferences.instance().isSystemLAFEnabled())
	{
	    final Logger logger = Logger.getLogger("Look and Feel");
	    try
	    {
		UIManager.setLookAndFeel(UIManager
			.getSystemLookAndFeelClassName());
	    }
	    catch (final Exception e)
	    {
		logger.log(Level.INFO, e.getMessage());
	    }
	}
	SwingUtilities.invokeLater(new Runnable()
	{
	    public void run()
	    {
		if (AbstractPreferences.instance().isConfigured())
		{
		    final MainUI main = new MainUI();

		    if (SystemTray.isSupported())
		    {
			try
			{
			    SystemTray
				    .getSystemTray()
				    .add(new PreampTray(
					    Toolkit.getDefaultToolkit()
						    .getImage(
							    ImageLoader
								    .getImageURL("audio-x-generic.png")),
					    main));
			}
			catch (final AWTException event)
			{
			    main.setVisible(true);
			}
		    }
		    else
		    {
			main.setVisible(true);
		    }
		}
		else
		{
		    new PreferencesUI(null);
		}
	    }
	});
    }

    /**
     * private constructor for masking the default constructor.
     */
    private Main()
    {
	super();
    }

}
